/**
 * 划分作用域的写法
 */
var counterBuilder = function () {
    function Counter(unit) {
        if (unit == null || unit == undefined) {
            unit = 1;
        }
        this.unit = unit;
        this.recordCount = 0;
        this.timerIdx = null;
    }
    Counter.prototype.addCount = function () {
        if (this.timerIdx == null) {
            return;
        }
        this.recordCount++;
    };
    /**
     * 注册周期统计
     * @param {*} callback 回调，传入参数是时间周期内触发的记录数
     */
    Counter.prototype.onTick = function (callback) {
        var self = this;
        //立即触发一次
        callback(self.recordCount);
        this.timerIdx = setInterval(function () {
            callback(self.recordCount);
            self.recordCount = 0;
        }, Math.ceil(this.unit * 1000));
    };
    /**
     * 关闭注册的周期统计
     */
    Counter.prototype.offTick = function () {
        if (this.timerIdx == null) {
            return;
        }
        clearInterval(this.timerIdx);
    };
    /**
     *
     * @param {*} unit 计时单位间隔 ，默认 1 ，即 1 秒
     */
    function innerBuilder(unit) {
        return new Counter(unit);
    }
    return innerBuilder;
};

/**
 * 构造器只需要实例化一个
 */
var newCounter = counterBuilder();
/**
 * 剩下的所有对象都通过构造器来获取，这样，Counter对象的范围就限制在了构造器内部
 */
var counter1 = newCounter();
var counter2 = newCounter(1);
counter1.onTick(function (total) {
    document.querySelector("#display1").innerText = total + " 次每秒";
});
counter2.onTick(function (total) {
    total *= 60;
    document.querySelector("#display2").innerText = total + "字每分钟";
});

function handleClickBtn1() {
    counter1.addCount();
}

function handleKeyDownText() {
    counter2.addCount();
}
